{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "45d65bcc-979a-4a3b-b6f9-77a5e8276091",
   "metadata": {},
   "source": [
    "# 任务\n",
    "手写 `Cart` 决策树"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cd838be-98d9-4cda-a4aa-45c747b8c3b8",
   "metadata": {},
   "source": [
    "### 导入必要的包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9955a328-c84a-402a-92af-a1931b6a6eb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import sys\n",
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8ce916e-ecd7-40bd-9a88-3a0de070f3c0",
   "metadata": {},
   "source": [
    "### 导入自己写的 `Cart` 决策树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bfb0f579-a370-4f24-b7ed-63e800e06a95",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将项目根目录添加到 Python 的模块搜索路径\n",
    "sys.path.append(os.path.abspath('../../'))  # 替换为项目根目录\n",
    "\n",
    "# 现在可以正常导入模块\n",
    "from ModelDefination.CARTDT.CARTDecisionTree import CARTDecisionTree"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "839d24d8-31c9-441b-a2bc-a972252741bd",
   "metadata": {},
   "source": [
    "### 训练决策树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "603f3ca1-e517-4560-9b24-735552374caa",
   "metadata": {},
   "outputs": [],
   "source": [
    "from joblib import dump, load\n",
    "\n",
    "final_train_data = pd.read_csv('../../FeatureSelectedData/selected_train.csv')\n",
    "final_X_train = final_train_data.iloc[:, :-1].values\n",
    "final_y_train = final_train_data.iloc[:, -1].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0bed3970-7496-4f63-9fc5-eb571350708e",
   "metadata": {},
   "outputs": [],
   "source": [
    "clf = CARTDecisionTree(max_depth=10)\n",
    "\n",
    "clf.fit(final_X_train, final_y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1150d306-c108-4634-ba7d-2775212d6d9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model saved as cart_model.joblib\n"
     ]
    }
   ],
   "source": [
    "dump(clf, '../../ModelFile/Cart/cart_model.joblib')\n",
    "print(\"Model saved as cart_model.joblib\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32e633f9-fb2f-40d1-b269-0ba521e653f2",
   "metadata": {},
   "source": [
    "### 将 `Cart` 决策树封装为 `Cart()` 函数进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "50edecd8-ad04-4059-a182-6e450af6c56b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def Cart(X):\n",
    "    clf = load('../../ModelFile/Cart/cart_model.joblib')\n",
    "    y_predicted = clf.predict(X)\n",
    "    return y_predicted"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8de3d27-63b4-42bd-b883-526c19d4b6e6",
   "metadata": {},
   "source": [
    "### 调用 `Cart()` 函数对测试集进行预测\n",
    "将预测结果输出为 `result_cart.csv`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e9444495-e7eb-41a4-9749-c14b90b556b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_test_data = pd.read_csv('../../FeatureSelectedData/selected_test.csv')\n",
    "result_file = pd.read_csv('../../RawData/sample_submission.csv')\n",
    "predictions = Cart(final_test_data)\n",
    "predictions_bool = (predictions == 1)\n",
    "result_file[\"Transported\"] = pd.Series(predictions_bool, result_file.index).astype(bool)\n",
    "result_file.to_csv('../../predictions/Cart/result_cart.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "399c7ecf-89a4-434a-a79a-4124965ca792",
   "metadata": {},
   "source": [
    "最终预测的结果保存在 `cart_svm.csv` 中"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
